package com.wms.service.impl;

import com.wms.entity.Resource;
import com.wms.entity.Role;
import com.wms.entity.RoleResource;
import com.wms.mapper.RoleMapper;
import com.wms.service.RoleResourceService;
import com.wms.service.RoleService;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleResourceService roleResourceService;


    @Override
    @Transactional
    public void deleteById(Long id) {
        Assert.notNull(id);

        removeById(id);
        roleResourceService.deleteByRoleId(id);
    }

    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        Assert.notEmpty(ids);

        removeByIds(ids);
        roleResourceService.deleteByRoleIds(ids);
    }

    @Override
    @Transactional
    public void bindResources(Role role, List<Resource> resourceList) {
        Assert.notNull(role);
        Assert.notEmpty(resourceList);

        roleResourceService.deleteByRoleId(role.getId());

        for (Resource resource : resourceList){
            RoleResource roleResource = new RoleResource();
            roleResource.setRoleId(role.getId());
            roleResource.setResourceId(resource.getId());
            roleResourceService.save(roleResource);
        }
    }
}
